package com.linzelin.controller;

import com.linzelin.mapper.UserMapper;
import com.linzelin.pojo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;

@RestController
public class MyController {

    @Resource
    private UserMapper userMapper;

    @RequestMapping("/insert")
    public Object insert(){
        User user = new User();
        user.setAge(18);
        user.setEmail("xxxxxxxx@qq.com");
        user.setName("lxs");
        return userMapper.insert(user);
    }

    @RequestMapping("/update")
    public Object update(){
        User user = new User();
        user.setId(6L);
        user.setEmail("yyyyyyyy@qq.com");
        user.setName("wan");
        return userMapper.updateById(user);
    }

    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id){
        //既然要测试删除操作肯定是要手动输入id的
      return null;
    }

    @RequestMapping("/hopelock/{id}")
    public Object optimisticLocker(@PathVariable("id") Integer id){
        /*1.查询用户信息*/
        User user = userMapper.selectById(id);

        /*2.2 模拟另一个线程执行插队操作*/
        User user_ = userMapper.selectById(id);
        user_.setName("gouxs");
        user_.setEmail("221231312@qq.com");
        userMapper.updateById(user_);

        /*2.修改用户信息*/
        user.setName("lxs");
        user.setEmail("222222222@qq.com");

        /*3.执行更新操作*/
        return userMapper.updateById(user);
    }

    @RequestMapping("/select/{id}")
    public Object selectById(@PathVariable("id") Integer id){
        return userMapper.selectById(id);
    }


    @RequestMapping("/selectByIds")
    public Object selectByBatchId(Integer[] ids){
        return userMapper.selectBatchIds(Arrays.asList(ids));
    }

    @RequestMapping("/selectByIdss")
    public Object selectByBatchIds(){
        HashMap<String,Object> map = new HashMap<>();
        // 自定义查询

        map.put("name","Jack");
        // 还记得学MyBatis的时候学的万能的Map吗?

        return userMapper.selectByMap(map);
    }
 }